GetOrAdd Method (TKey, Func(TKey, TValue))

Task Parallel System.Threading

Adds a key/value pair to the ConcurrentDictionary<(Of <(TKey, TValue>)>) if the key does not already exist.

Namespace:  System.Collections.Concurrent
Assembly:  System.Threading (in System.Threading.dll)

Syntax

Visual Basic (Declaration)
Public Function GetOrAdd ( _
	key As TKey, _
	valueFactory As Func(Of TKey, TValue) _
) As TValue
C#
public TValue GetOrAdd(
	TKey key,
	Func<TKey, TValue> valueFactory
)

Parameters

key
Type: TKey
The key of the element to add.
valueFactory
Type: System..::.Func<(Of <(TKey, TValue>)>)
The function used to generate a value for the key

Return Value

The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value for the key as returned by valueFactory if the key was not in the dictionary.

Exceptions

ExceptionCondition
System..::.ArgumentNullExceptionkey is a null reference (Nothing in Visual Basic).
System..::.ArgumentNullExceptionvalueFactory is a null reference (Nothing in Visual Basic).
System..::.OverflowExceptionThe dictionary contains too many elements.

See Also